using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameManager : MonoBehaviour {




    public GameObject gamegui;
    public float m_EnemyAcurracy = 0.65f;
    public float m_MovingAimReduction = 0.15f;
    public float m_SprintingAimReduction = 0.25f;

    static private GameManager instance = null;
    private bool m_Paused;
    static public GameManager GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
        CreateWeapons();
        Time.timeScale = 1;
    }
	void Start() 
    {

        QuestCreator.GetInstance().InitializeQuests();
        gamegui.SetActive(true);
        SoundManager.Instance.PlaySound("BgMusic");
	}
	
	void Update () 
    {

        if (m_GameEnded && timer < GameOverTime)
        {
            timer += Time.deltaTime;
            if(timer >= GameOverTime)
            {
                OpenGameOverMenu();
            }
        }
	
	}
    void CreateWeapons()
    {
        WeaponManager.GetInstance().CreateWeaponsTemplate();

    }
    void OnGUI()
    {
        QuestSystem.GetInstance().ShowQuests();
    }

    float GameOverTime = 2.0f;

    bool m_GameEnded = false;
    float timer = 0.0f;
    public bool IsGameFinished() { return m_GameEnded; }
	public void LoseGame()
	{
        m_Paused = true;
        CameraOmniVision.GetInstance().ActivateRedShader();
        m_GameEnded = true;
	}
    public void OpenGameOverMenu()
    {
        Time.timeScale = 0;
        PopUpManager.instance.ShowMenu(true, PopUpManager.POPUP_MENUS.GAMEOVER);

    }
    public void WinGame()
    {
        m_Paused = true;
        CameraOmniVision.GetInstance().ActivateGreyShader();
        CharacterDataManager.GetInstance().AddAditionalPoints(2);
        m_GameEnded = true;
    }
    /// <summary>
    /// Llama a todas las funciones que tiene que llamar al terminar la escena para resetear valores y etc.
    /// </summary>
    public void OnEndScene()
    {
        CharacterDataManager.GetInstance().OnEndGame();
        QuestCreator.GetInstance().ResetInstance();
        QuestSystem.GetInstance().ResetInstance();
        Time.timeScale = 1;
        CameraOmniVision.GetInstance().DeactiveShaders();

        PopUpManager.instance.ShowMenu(false, PopUpManager.POPUP_MENUS.GAMEOVER);
        if (InventoryController.GetInstance().IsOpened())
        {
            InventoryController.GetInstance().OpenInventory();
        }
        if (AttributesController.GetInstance().IsOpened())
        {
            AttributesController.GetInstance().ShowGUI(false, false);
        }
        PopUpManager.instance.ShowMenu(false, PopUpManager.POPUP_MENUS.PAUSE);
        //vuelve al menu por ahora
        Application.LoadLevel(0);
    }
    public void Pause()
    {
        if (m_Paused == false)
        {
            if (InventoryController.GetInstance().IsOpened())
            {
                InventoryController.GetInstance().OpenInventory();
            }
            if (AttributesController.GetInstance().IsOpened())
            {
                AttributesController.GetInstance().ShowGUI(false, false);
            }
            Time.timeScale = 0;
            m_Paused = true;
            PopUpManager.instance.ShowMenu(true, PopUpManager.POPUP_MENUS.PAUSE);
            CameraOmniVision.GetInstance().ActivateGreyShader();

        }
        else
        {
            Time.timeScale = 1;
            m_Paused = false;
            PopUpManager.instance.ShowMenu(false, PopUpManager.POPUP_MENUS.PAUSE);
            CameraOmniVision.GetInstance().DeactiveShaders();
        }
    }
    public bool IsPaused()
    {
        return m_Paused;
    }
}
